<template>
  <div class="sjplatformAdd">
    <el-form label-width="100px">
      <el-form-item label="项目名称：">
        <el-input v-model="projectStore.projectModel.projectName" />
      </el-form-item>
      <el-form-item label="项目依据：">
        <el-input v-model="projectStore.projectModel.basis" type="textarea" />
      </el-form-item>
      <el-form-item label="项目描述：">
        <el-input v-model="projectStore.projectModel.description" type="textarea" />
      </el-form-item>
      <el-form-item label="开始日期：">
        <el-date-picker v-model="projectStore.projectModel.startTime" type="date" placeholder="选择开始日期" />
      </el-form-item>
      <el-form-item label="结束日期：">
        <el-date-picker v-model="projectStore.projectModel.endTime" type="date" placeholder="选择开始日期" />
      </el-form-item>
      <el-form-item label="项目发起人：">
        <el-input v-model="projectStore.projectModel.initiatorName" @click="openUserSelectForm('选择项目发起人', false)"
          clearable @clear="clearinitiator" />
      </el-form-item>
      <el-form-item label="项目经理：">
        <el-input v-model="projectStore.projectModel.managerName" @click="openUserSelectForm('选择项目经理', true)" clearable
          @clear="clearmanager" />
      </el-form-item>
      <el-form-item label="立项文档：">
        <FileUpload ref="fileUpload" :historyFileList="historyFileList" @file-delete="handleFileDeleted" />
      </el-form-item>
    </el-form>
  </div>

  <el-dialog v-model="isOpenUserSelectForm" :title="formTitle" width="50%" draggable :destroy-on-close="true">
    <UserSelect :initSelectedUser="initSelectedUser" />
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref, onUnmounted, reactive } from "vue";
import UserSelect from "../../../components/SystemManager/User/UserSelect.vue";
import { useProjectStore } from "../../../store/useProjectStore";
import { useProjectTable } from "../../../hooks/ProjectManager/Start/useProjectTable";
import emitter from "../../../utils/emitter";
import FileUpload from "../../Common/FileUpload.vue";

let formTitle = ref("");
let fileUpload = ref(null);
let historyFileList = ref();

let projectStore = useProjectStore();

let isOpenUserSelectForm = ref(false); //是否打开用户选择窗口
let initiatorSelected = [];
let managerSelected = [];
if (projectStore.projectModel.managerId) {
  managerSelected.push(projectStore.projectModel.managerId);
}
//初始化项目发起人
let { projectTable, getProjectStakeholder, getProjectDoc, deleteProjectDoc } = useProjectTable(1, 10);
if (projectStore.projectModel.id) {
  getProjectStakeholder(projectStore.projectModel.id, 1).then(() => {
    let stakeholderId = [];
    let stakeholderName = [];
    for (let item of projectTable.stakeholderList) {
      stakeholderId.push(item.userId);
      stakeholderName.push(item.realName);
    }
    projectStore.projectModel.initiatorName = stakeholderName.join(",");
    projectStore.projectModel.initiatorId = stakeholderId.join(",");

    initiatorSelected = stakeholderId;
  });
  getProjectDoc(projectStore.projectModel.id, 1).then(() => {
    historyFileList.value = projectTable.documentList;
  });
}

let initSelectedUser = reactive({
  single: false,
  selectedrow: [],
});

function clearinitiator() {
  projectStore.projectModel.initiatorName = "";
  projectStore.projectModel.initiatorId = "";
  initiatorSelected = [];
}

function clearmanager() {
  projectStore.projectModel.managerName = "";
  projectStore.projectModel.managerId = "";
  managerSelected = [];
}

function openUserSelectForm(title, value) {
  formTitle.value = title;
  initSelectedUser.single = value;
  if (title == "选择项目发起人") {
    initSelectedUser.selectedrow = initiatorSelected;
  }
  if (title == "选择项目经理") {
    initSelectedUser.selectedrow = managerSelected;
  }
  isOpenUserSelectForm.value = true;
}
function cancel() {
  isOpenUserSelectForm.value = false;
}
function confirm() {
  emitter.emit("confirmSelectedUser");
}

//供用户选择调用设置选择的项
emitter.on("set-selected-user", (SelectionRows: any) => {
  let selectedUserId = [];
  if (formTitle.value == "选择项目发起人") {
    let selectedUserName = [];
    for (let item of SelectionRows) {
      selectedUserId.push(item.id);
      selectedUserName.push(item.realName);
    }
    initiatorSelected = selectedUserId;

    projectStore.projectModel.initiatorName = selectedUserName.join(",");
    projectStore.projectModel.initiatorId = selectedUserId.join(",");
  }
  if (formTitle.value == "选择项目经理") {
    managerSelected = [];
    managerSelected.push(SelectionRows.id);
    projectStore.projectModel.managerId = SelectionRows.id;
    projectStore.projectModel.managerName = SelectionRows.realName;
  }
  isOpenUserSelectForm.value = false;
});

// 处理文件删除事件
async function handleFileDeleted(objectId) {
  if (await deleteProjectDoc(objectId)) {
    historyFileList.value = historyFileList.value.filter(file => file.objectId !== objectId);
  }
}

onUnmounted(() => {
  //组件卸载后解绑
  emitter.off("set-selected-user");
});

</script>